package com.lyq.pdf.itext;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import util.json.JSONUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class ReadPdfByPage {
    public static String readPdfByPage(String fileName) throws IOException {
        long start = System.currentTimeMillis();

        String result = "";
        URL url = new URL("https://img-test.aixuexi.com/B:1006:K/1680710400/281100a979c640de816b22f52daa564b.pdf");
//        File file = new File(fileName);
        FileInputStream in = null;
        try {
//            in = new FileInputStream(fileName);
            // 新建一个PDF解析器对象
//            PdfReader reader = new PdfReader(fileName);
            PdfReader pdfReader = new PdfReader(url);
            pdfReader.setAppendable(true);
            // 对PDF文件进行解析，获取PDF文档页码
            int size = pdfReader.getNumberOfPages();
            for(int i = 1 ; i < size + 1; ){
                //一页页读取PDF文本
                String pageStr = PdfTextExtractor.getTextFromPage(pdfReader,i);
                result = result + pageStr + "\n" + "PDF解析第"+ (i)+ "页\n";
                i= i+1;
            }
            pdfReader.close();
        } catch (Exception e) {
//            System.out.println("读取PDF文件" + file.getAbsolutePath() + "生失败！" + e);
            System.out.println("读取PDF文件生失败！" + e);
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }

        long end = System.currentTimeMillis();
        System.out.println("总耗时：" + (end - start));
        return result;
    }

    private static Map<String, float[]> getKeyWords(String keyWords) throws IOException {
        long start = System.currentTimeMillis();

        URL url = new URL("https://q-independent.aixuexi.com/B:1006:K/1681142400/5f70b0f5b7f14299a21043cbcfd3b7a2.pdf");
        PdfReader pdfReader = new PdfReader(url);

        Map<String, float[]> coordinates = new HashMap<>();
        int page;
        try {
            int pageNum = pdfReader.getNumberOfPages();
            PdfReaderContentParser pdfReaderContentParser = new PdfReaderContentParser(pdfReader);
            CustomRenderListener renderListener = new CustomRenderListener();
            renderListener.setKeyWord(keyWords);
            for (page = 1; page <= pageNum; page++) {
                renderListener.setPage(page);
                pdfReaderContentParser.processContent(page, renderListener);
                float[] coordinate = renderListener.getPcoordinate();
                //解析内容不为空时，将内容和坐标存入map中
                if (null != coordinate) {
                    coordinates.put(renderListener.getTextContent(), coordinate);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("总耗时：" + (end - start));
        return coordinates;
    }

    public static void main(String[] args) throws IOException {
//        String s = ReadPdfByPage.readPdfByPage("");
//        System.out.println(s);

        Map<String, float[]> map = ReadPdfByPage.getKeyWords("知识点");
        System.out.println(JSONUtils.json(map));
    }
}
